<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">医疗系统登录</h2>
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入账号"
              prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              @click="handleLogin"
              class="login-button"
              :loading="loading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref(null)


const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (!valid) return

    loading.value = true

    setTimeout(() => {
      if (loginForm.value.username === 'ADMIN' && loginForm.value.password === 'ADMIN') {
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('userRole', 'admin')
        router.push('/')
      } else if (loginForm.value.username === 'doctor' && loginForm.value.password === 'doctor') {
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('userRole', 'doctor')
        router.push('/doctors')
      } else {
        ElMessage.error('账号或密码错误')
      }
      loading.value = false
    }, 500)
  })

}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-card {
  width: 400px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409eff;
}

.login-button {
  width: 100%;
}
</style>